package com.kobe.util;

import java.util.ArrayList;
import java.util.List;

public class MathUtil {

    public static int[] formatNumberToArray(int number, int length) {
        int[] returnValue = new int[length];
        for (int i = 0; i < length; i++) {
            returnValue[length - 1 - i] = number % 10;
            number /= 10;
        }
        return returnValue;
    }

    private static void perm(int[] arr, int k, int m, List<int[]> list) {
        if (k == m) {
            list.add(arr.clone());
        } else {
            for (int i = k; i <= m; i++) {
                arr[k] = (arr[k] + arr[i]) - (arr[i] = arr[k]);
                perm(arr, k + 1, m, list);
                arr[k] = (arr[k] + arr[i]) - (arr[i] = arr[k]);
            }
        }
    }

    public static List<int[]> getPerm(int[] arr) {
        List<int[]> list = new ArrayList<int[]>();
        perm(arr, 0, arr.length - 1, list);
        return list;
    }

    public static boolean isPrime(int number) {
        number = number < 0 ? number * (-1) : number;
        if (number == 1) {
            return false;
        }
        int sqrt = ((Double) Math.sqrt(number)).intValue();
        boolean isPrime = true;
        if (number % 2 == 0 && number != 2) {
            isPrime = false;
        } else {
            for (int i = 3; i <= sqrt; i += 2) {
                if (number % i == 0) {
                    isPrime = false;
                    break;
                }
            }
        }
        return isPrime;
    }

    static List<Integer> primes = new ArrayList<Integer>();

    static {
        for (int i = 3; i < 200000; i += 2) {
            if (isPrime(i))
                primes.add(i);
        }
    }

    static int[] factors = new int[30];

    public static int[] getFactors(int number) {
        int factor = 2;
        int factorCount = 0;

        int size = 30;
        int index = 0;
        int primesIndex = 0;
        int increment = 10;

        while (number > 1) {
            if (number % factor == 0) {
                factorCount++;
                factors[index++] = factor;
                number /= factor;
                if (index == size) {
                    int[] tempFactors = new int[size + increment];
                    System.arraycopy(factors, 0, tempFactors, 0, size);
                    size += increment;
                    factors = tempFactors;
                }
            } else {
                factor = primes.get(primesIndex);
                primesIndex++;
            }
        }

        int[] returnFactors = new int[factorCount];
        System.arraycopy(factors, 0, returnFactors, 0, factorCount);
        return returnFactors;
    }

    public static double getFactorial(int start, int number) {
        double result = 1D;
        for (int i = start; i <= number; i++) {
            result *= i;
        }
        return result;
    }

    public static double getFactorial(int number) {
        return getFactorial(1, number);
    }

    public static void main(String[] args) {

    }
}
